package fr.vahren.dotastrat.game;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class D2SGameServer {

	private static final int SERVER_PORT = 19025;

	private Map<Integer, D2SGame> games = new HashMap<>();

	public static void main(String[] args) {
		// start server loop

		D2SGameServer server = new D2SGameServer();

		server.startServer();
	}

	private ServerSocket socket;

	public void startServer() {

		try {
			// start listening
			socket = new ServerSocket(SERVER_PORT);

			while (true) {

				Socket client = socket.accept();
				createNewThread(client);

			}

		} catch (IOException e) {
			System.err.println("Error occured");
			e.printStackTrace();
		}

	}

	private void createNewThread(Socket client) {
		D2SGameServerThread thread = new D2SGameServerThread(client, games);
		thread.start();
	}

	public void addGame(D2SGame game) {
		games.put(game.getId(), game);
	}

}
